package com.lanou.dllo.findfood.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.dllo.findfood.activity.FindViewPagerlastActivity;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.volley.VolleySington;

/**
 * Created by dllo on 15/10/28.
 */
public class FindTopFragemnt extends Fragment implements View.OnClickListener {
    private String title;// 标题
    private String imgUrl;// 图片地
    private NetworkImageView healthImg;
    private TextView healthTv;
    private  int id;

    private  static  FindTopFragemnt topFragemnt;

    private  FindTopFragemnt(){

    }
    public static FindTopFragemnt getInstance(String title, String imgUrl,int id){
FindTopFragemnt topFragemnt = new FindTopFragemnt();
        Bundle args = new Bundle();
        args.putString("title", title);//以键值的形式对文字进行获取  用putString(健  ,  数据)的形式传递数据
        args.putString("imgUrl", imgUrl);
        args.putInt("id",id);
        topFragemnt.setArguments(args);
        return  topFragemnt;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            title = bundle.getString("title"); //使用Bundle读取数据 通过键的形式
            imgUrl = bundle.getString("imgUrl");//获取参数
            id = bundle.getInt("id");
        }



    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.find_top_viewpeger,null);
        healthImg = (NetworkImageView) view.findViewById(R.id.healthImg);
        healthTv = (TextView) view.findViewById(R.id.healthTv);
        healthTv .setText(title);
        healthImg.setOnClickListener(this);
        getBitmap(imgUrl, healthImg);
        return view;
    }
    private void getBitmap(String url, final NetworkImageView networkImageView) {
        ImageLoader imageLoader = VolleySington.getInstance().getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.healthImg:
                Intent intent = new Intent(getActivity(), FindViewPagerlastActivity.class);
                intent.putExtra("id",id);
                startActivity(intent);

            break;
            default:
                break;
        }
    }
}

